package com.ruoyi.appinterface.controller;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MessageUtil;
import com.ruoyi.common.utils.WXUrlUtils;
import com.ruoyi.system.domain.CompanyProfile;
import com.ruoyi.system.domain.Featured;
import com.ruoyi.system.domain.PopularScience;
import com.ruoyi.system.domain.SubscribeDate;
import com.ruoyi.system.domain.tumor.ReportRecord;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.tumor.IOrderDetailService;
import com.ruoyi.system.service.tumor.IReportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/appcommon")
public class AppCommonController extends BaseController {

    @Autowired
    WXUrlUtils wxUrlUtils;

    @Autowired
    private IReportRecordService reportRecordService;

    @Autowired
    private IFeaturedService featuredService;

    @Autowired
    private ICompanyProfileService companyProfileService;

    @Autowired
    private IPopularScienceService popularScienceService;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private MessageUtil messageUtil;

    @Autowired
    IOrderDetailService iOrderDetailService;

    @Autowired
    private ISubscribeDateService subscribeDateService;

    /*
        获取所有可预约日期
    */
    @GetMapping("/getSubscribeDateList")
    public TableDataInfo getSubscribeDateList()
    {
        List<SubscribeDate> list = subscribeDateService.getSubscribeDateList();
        return getDataTable(list);
    }


    /*
        科普知识分类查询(不传type，代表全部)
     */
    @PostMapping("/getPopularScienceList")
    public TableDataInfo getPopularScienceList(@RequestBody Map map)
    {
        List<PopularScience> list = popularScienceService.getPopularScienceList(map);
        return getDataTable(list);
    }

    /**
     * 查询订单记录列表(查的是oms库表的数据)
     */
    @PostMapping("/selectOmsOrderListByPhone")
    public TableDataInfo selectOmsOrderListByPhone(@RequestBody Map map)
    {
        List<Map> list = iOrderDetailService.selectOmsOrderListByPhone(map);
        return getDataTable(list);
    }



    // 获取验证码
    @GetMapping(value = "/code/{phone}")
    public AjaxResult getCode(@PathVariable String phone) {
        // 验证码长度
        int codeLength = 4;
        String code = generateRandomCode(codeLength);
        com.alibaba.fastjson2.JSONObject obj = new com.alibaba.fastjson2.JSONObject();
        obj.put("code", code);
        try {
            messageUtil.sendSms(phone, "SMS_196270385", obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(code);
    }



    // 登录校验手机号
    @GetMapping("/checkPhone/{phone}")
    public AjaxResult checkPhone(@PathVariable("phone") String phone) {
        SysUser sysUser = userService.selectUserByPhonenumber(phone);
        if(sysUser == null ){ //不存在，则注册
            SysUser sysUser1 = new SysUser();
            sysUser1.setDelFlag("0");
            sysUser1.setPhonenumber(phone);
            userService.insertUser(sysUser1);
            return AjaxResult.success(sysUser1);
        }else if(sysUser !=null ){ // 存在
            return AjaxResult.success(sysUser);
        }
        return AjaxResult.success(sysUser);
    }

    /**
     * 生成指定长度的随机数验证码
     *
     * @param length 验证码长度
     * @return 随机数验证码
     */
    public static String generateRandomCode(int length) {
        String code = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            code += random.nextInt(10);
        }
        return code;
    }


    @GetMapping("/getPhoneNumber/{code}")
    public AjaxResult getPhoneNumber(@PathVariable("code") String code) {
        JSONObject object=wxUrlUtils.getPhoneNumber(code);
        return AjaxResult.success(object);
    }


    /**
     * 查询报告记录列表(先查的是lims表的数据)
     */
    @PostMapping("/selectLimsReportListByPhone")
    public TableDataInfo selectLimsReportListByPhone(@RequestBody Map map)
    {
        List<Map> list = reportRecordService.selectLimsReportListByPhone(map);
        return getDataTable(list);
    }

    //查询预约解读报告
    @GetMapping("/selectLimsReportByOrderNo/{orderNo}")
    public AjaxResult selectLimsReportByOrderNo(@PathVariable String orderNo)
    {
        return AjaxResult.success(reportRecordService.selectLimsReportByOrderNo(orderNo));
    }



    /**
     * 保存报告记录（新增或者修改）
     */

    @PostMapping("/reservation")
    public AjaxResult add(@RequestBody ReportRecord reportRecord)
    {
        return toAjax(reportRecordService.saveReportRecord(reportRecord));
    }


    /**
     * 查询特色产品列表
     */
    @GetMapping("/getFeaturedList")
    public TableDataInfo getFeaturedList()
    {
        Featured featured = new Featured();
        List<Featured> list = featuredService.selectFeaturedList(featured);
        return getDataTable(list);
    }

    /**
     * 获取特色产品详情
     */
    @GetMapping(value = "/getFeatured/{id}")
    public AjaxResult getFeatured(@PathVariable("id") Long id)
    {
        return success(featuredService.selectFeaturedById(id));
    }

    /**
     * 查询公司简介列表
     */
    @PostMapping("/companyProfileList")
    public TableDataInfo companyProfileList(@RequestBody CompanyProfile companyProfile)
    {
        companyProfile.setStatus("1");
        List<CompanyProfile> list = companyProfileService.selectCompanyProfileList(companyProfile);
        return getDataTable(list);
    }


    /**
     * 获取科普知识详细信息
     */
    @GetMapping(value = "/getPopularScience/{id}")
    public AjaxResult getPopularScience(@PathVariable("id") Long id)
    {
        return success(popularScienceService.selectPopularScienceById(id));
    }

    /**
     * 取消预约
     */
    @GetMapping(value = "/cancelReservation/{id}")
    public AjaxResult cancelReservation(@PathVariable("id") Integer id)
    {
        return success(reportRecordService.cancelReservation(id));
    }



    //根据字段类型查询字典
    @GetMapping("/getSysDictTypeList/{dictType}")
    public TableDataInfo list(@PathVariable String dictType)
    {
        List<SysDictData> list = dictTypeService.selectDictDataByType(dictType);
        return getDataTable(list);
    }



}
